package day23;

// 1. 哪些代码是多线程运行代码
// 2. 明确共享数据
// 3. 明确多线程运行代码中哪些语句是操作共享数据的
public class Demo05 {
	public static void main(String[] args) {
		Bank bank = new Bank();

		Customer c1 = new Customer(bank);
		Customer c2 = new Customer(bank);
		Customer c3 = new Customer(bank);

		new Thread(c1).start();
		new Thread(c2).start();
		new Thread(c3).start();

	}
}

// ----------

// 例: 银行有一个金库, 有三个用户向银行存钱
// 每个用户分5次存入, 每次存入100

class Bank {
	private int sum;

	public /*synchronized*/ void add(int money) {
		synchronized (this) {
			this.sum += money;
			System.out.println("sum: " + this.sum);
		}
	}
}

class Customer implements Runnable {

	private Bank bank;

	public Customer(Bank bank) {
		super();
		this.bank = bank;
	}

	@Override
	public void run() {
		for (int i = 0; i < 5; i++) {
			bank.add(100);
		}
	}

}